package com.konne.nightmare.FastPublicOpinion.mvp.presenter;


import android.graphics.Path;

import com.google.gson.Gson;
import com.konne.nightmare.FastPublicOpinion.base.BaseMvpPresenter;
import com.konne.nightmare.FastPublicOpinion.base.OnLoadDataListener;
import com.konne.nightmare.FastPublicOpinion.bean.BaseBean;
import com.konne.nightmare.FastPublicOpinion.bean.HomeBean;
import com.konne.nightmare.FastPublicOpinion.http.BaseResponse;
import com.konne.nightmare.FastPublicOpinion.bean.OpinionDataBean;
import com.konne.nightmare.FastPublicOpinion.mvp.model.IHomeFragmentModel;
import com.konne.nightmare.FastPublicOpinion.mvp.model.impl.HomeFragmentModelImpl;
import com.konne.nightmare.FastPublicOpinion.mvp.view.IHomeFragmentView;

import java.util.List;


public class HomePresenter extends BaseMvpPresenter<IHomeFragmentView> {
    private IHomeFragmentModel iEnableActivityModel;

    public HomePresenter() {
        iEnableActivityModel = new HomeFragmentModelImpl();
    }

    private int index = 0;

    public void onRefresh(boolean isScreen) {
        index = 1;
        myWarning(true, isScreen);
    }

    public void onLoadMore(boolean isScreen) {
        index++;
        myWarning(false, isScreen);
    }

    private void myWarning(boolean isUpRefresh, boolean isScreen) {
        if (mView == null) return;

        iEnableActivityModel.myWarning(index, 20,
                new OnLoadDataListener<List<HomeBean.RowsBean>>() {
                    @Override
                    public void onSuccess(BaseResponse<List<HomeBean.RowsBean>> t) {
                        getView().myWarning(t, isUpRefresh);
                            if (index * 20 > t.total)
                                mView.notMoreData();
                                getView().cancelLoadDialog();
                    }

                    @Override
                    public void onFailure(String error) {
                        getView().cancelLoadDialog();
                    }
                });

    }


    /**
     * 舆情列表数据
     */
    int opinionIndex = 0;

    public void onOpinionOnRefresh(){
        opinionIndex = 1;
        postOpinionList(true);
    }

    public void onOpinionOnLoadMore(){
        opinionIndex++;
        postOpinionList(false);
    }


    private void postOpinionList(boolean isUpRefresh){
        if (mView == null) return;

        BaseBean.NullRequestBean bean = new BaseBean.NullRequestBean();

        iEnableActivityModel.postOpinionList(opinionIndex, 20, new Gson().toJson(bean), new OnLoadDataListener<List<OpinionDataBean.ResponseDataBean>>() {
            @Override
            public void onSuccess(BaseResponse<List<OpinionDataBean.ResponseDataBean>> t) {
                mView.postOpinionList(t, isUpRefresh);
                if (opinionIndex * 20 > t.total) mView.notMoreData();
            }

            @Override
            public void onFailure(String error) {
                mView.responseFailed(error);
            }
        });
    }


    public void postOpinionAddCollect(int id){
        if (mView == null) return;

        OpinionDataBean.RequestIdDataBean bean = new OpinionDataBean.RequestIdDataBean();
        bean.setIds(id);

        iEnableActivityModel.postOpinionAddCollect(new Gson().toJson(bean), new OnLoadDataListener<String>() {
            @Override
            public void onSuccess(BaseResponse<String> t) {
                mView.postOpinionCWBD(t);
            }

            @Override
            public void onFailure(String error) {
                mView.responseFailed(error);
            }
        });
    }

    public void postOpinionCancelCollect(int id){
        if (mView == null) return;

        OpinionDataBean.RequestIdDataBean bean = new OpinionDataBean.RequestIdDataBean();
        bean.setIds(id);

        iEnableActivityModel.postOpinionCancelCollect(new Gson().toJson(bean), new OnLoadDataListener<String>() {
            @Override
            public void onSuccess(BaseResponse<String> t) {
                mView.postOpinionCWBD(t);
            }

            @Override
            public void onFailure(String error) {
                mView.responseFailed(error);
            }
        });
    }

    public void postOpinionAddWarning(int id){
        if (mView == null) return;

        OpinionDataBean.RequestIdDataBean bean = new OpinionDataBean.RequestIdDataBean();
        bean.setIds(id);

        iEnableActivityModel.postOpinionAddWarning(new Gson().toJson(bean), new OnLoadDataListener<String>() {
            @Override
            public void onSuccess(BaseResponse<String> t) {
                mView.postOpinionCWBD(t);
            }

            @Override
            public void onFailure(String error) {
                mView.responseFailed(error);
            }
        });
    }

    public void postOpinionCancelWarning(int id){
        if (mView == null) return;

        OpinionDataBean.RequestIdDataBean bean = new OpinionDataBean.RequestIdDataBean();
        bean.setIds(id);

        iEnableActivityModel.postOpinionCancelWarning(new Gson().toJson(bean), new OnLoadDataListener<String>() {
            @Override
            public void onSuccess(BaseResponse<String> t) {
                mView.postOpinionCWBD(t);
            }

            @Override
            public void onFailure(String error) {
                mView.responseFailed(error);
            }
        });
    }

    public void postOpinionAddBriefing(int id){
        if (mView == null) return;

        OpinionDataBean.RequestIdDataBean bean = new OpinionDataBean.RequestIdDataBean();
        bean.setIds(id);

        iEnableActivityModel.postOpinionAddBriefing(new Gson().toJson(bean), new OnLoadDataListener<String>() {
            @Override
            public void onSuccess(BaseResponse<String> t) {
                mView.postOpinionCWBD(t);
            }

            @Override
            public void onFailure(String error) {
                mView.responseFailed(error);
            }
        });
    }

    public void postOpinionCancelBriefing(int id){
        if (mView == null) return;

        OpinionDataBean.RequestIdDataBean bean = new OpinionDataBean.RequestIdDataBean();
        bean.setIds(id);

        iEnableActivityModel.postOpinionCancelBriefing(new Gson().toJson(bean), new OnLoadDataListener<String>() {
            @Override
            public void onSuccess(BaseResponse<String> t) {
                mView.postOpinionCWBD(t);
            }

            @Override
            public void onFailure(String error) {
                mView.responseFailed(error);
            }
        });
    }

    public void postOpinionDelete(int id){
        if (mView == null) return;

        OpinionDataBean.RequestIdDataBean bean = new OpinionDataBean.RequestIdDataBean();
        bean.setIds(id);

        iEnableActivityModel.postOpinionDelete(new Gson().toJson(bean), new OnLoadDataListener<String>() {
            @Override
            public void onSuccess(BaseResponse<String> t) {
                mView.postOpinionCWBD(t);
            }

            @Override
            public void onFailure(String error) {
                mView.responseFailed(error);
            }
        });
    }

    public void postOpinionDataStatistics(int type){
        if (mView == null) return;

        OpinionDataBean.RequestTypeDataBean bean = new OpinionDataBean.RequestTypeDataBean();
        bean.setType(type);

        iEnableActivityModel.postOpinionDataStatistics(new Gson().toJson(bean), new OnLoadDataListener<OpinionDataBean.ResponseStatisticalDataBean>() {
            @Override
            public void onSuccess(BaseResponse<OpinionDataBean.ResponseStatisticalDataBean> t) {
                mView.postOpinionDataStatistics(t);
            }

            @Override
            public void onFailure(String error) {
                mView.responseFailed(error);
            }
        });
    }
}
